package com.clearliang.nativeapp.plugins;

import android.content.Context;
import android.util.Log;

import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.view.FlutterView;

/**
 * Created by ClearLiang on 2019/6/4
 * Description：
 * Function :
 */
public class BasicPlugin implements BasicMessageChannel.MessageHandler {
    private static final String TAG = "Basic信息：";
    /**
     * 通道名称、函数名、参数名
     */
    public final static String CHANNEL = "com.liang.flutter.plugin/basic";

    static BasicMessageChannel messageChannel;

    private Context context;

    public BasicPlugin(Context context) {
        this.context = context;
    }

    public static void registerWith(FlutterView flutterView) {
        messageChannel = new BasicMessageChannel(flutterView, CHANNEL, StandardMessageCodec.INSTANCE);
        BasicPlugin flutterPluginBasicTest = new BasicPlugin(flutterView.getContext());
        messageChannel.setMessageHandler(flutterPluginBasicTest);
    }

    /**
     * java 发起通信
     */
    void sendMessage(String string) {
        messageChannel.send(string, new BasicMessageChannel.Reply() {
            @Override
            public void reply(Object o) {
                Log.e(TAG, "reply: " + 0);
            }
        });
    }


    /**
     * Flutter 发起的通信
     */
    @Override
    public void onMessage(Object o, BasicMessageChannel.Reply reply) {
        Log.e(TAG, "onMessage: " + o);
        reply.reply("ok");
    }
}
